#include <myheader.h>
#include <shadow.h>
#include <sys/epoll.h>
#include <crypt.h>
enum TaskType{
    TASK_LOGIN1_SECTION1 = 1,
    TASK_LOGIN1_SECTION1_RESP_OK,
    TASK_LOGIN1_SECTION1_RESP_ERROR,
    TASK_LOGIN1_SECTION2,
    TASK_LOGIN1_SECTION2_RESP_OK,
    TASK_LOGIN1_SECTION2_RESP_ERROR,
};

/*Usage:  */
void build_tlv(uint16_t tag, const char *value, uint8_t *buffer, size_t *buffer_size) {
    size_t offset = 0;
    uint32_t length = strlen(value);

    // 添加Tag
    buffer[offset++] = (tag >> 8) & 0xFF;
    buffer[offset++] = tag & 0xFF;

    // 添加Length
    buffer[offset++] = (length >> 24) & 0xFF;
    buffer[offset++] = (length >> 16) & 0xFF;
    buffer[offset++] = (length >> 8) & 0xFF;
    buffer[offset++] = length & 0xFF;

    // 添加Value
    memcpy(&buffer[offset], value, length);
    offset += length;

    *buffer_size = offset;
}


int main(int argc, char *argv[])
{
    //struct spwd *sp = getspnam("lwj");
    //printf("%s\n",sp->sp_pwdp);
    

    int sfd = socket(AF_INET, SOCK_STREAM,0);
    ERROR_CHECK(sfd,-1,"socket");

    //创建sockaddr对象
    struct sockaddr_in client_addr;
    memset(&client_addr, 0, sizeof(client_addr));
    client_addr.sin_addr.s_addr = inet_addr("192.168.112.129");
    client_addr.sin_family = AF_INET;
    client_addr.sin_port = htons(8080);
    int ret = connect(sfd, (struct sockaddr *) &client_addr, sizeof(client_addr));
    ERROR_CHECK(ret, -1, "connect");

    //stdin
    //int rec_cnt = read(STDIN_FILENO, buf, sizeof(buf));
    //ERROR_CHECK(rec_cnt, -1,"read");
    //发送

    char sendbuf[1024] = {0};
    memset(sendbuf, 0,sizeof(sendbuf));
    const char *msg = "lwj"; 
    size_t buf_size;
    build_tlv(TASK_LOGIN1_SECTION1,msg,sendbuf,&buf_size);
    printf("epoll_chatroom_client 74 main(), send msg : %s \n", sendbuf);
    int send_cnt = send(sfd, sendbuf, buf_size,0);
    ERROR_CHECK(send_cnt, -1, "send");

    close(sfd);
    return 0;
}

